# Controlls the addition and removal of adminnstrators from projects

class AdministratorsController < ApplicationController
  include EnotifyLdap
  layout 'admin_standard'
  before_filter :authenticate
  #before_filter :get_project_context
  active_scaffold :administrator
  active_scaffold :administrator do |config|
       config.columns = [:person]
       config.columns[:person].label = "Administrators"
       config.actions.exclude :update, :show, :create       
  end
  
  
  def for_project
    @project = Project.find(params[:id])
    
    respond_to do |format|
      format.html 
    end
  end

  # GET /administrators
  # GET /administrators.xml
  def index
    @administrators = Administrator.find(:all)

    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @administrators }
    end
  end


  # GET /administrators/new
  # GET /administrators/new.xml
  def new
    @administrator = Administrator.new
    @project = Project.find(params[:id])
    
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @administrator }
    end
  end

  # GET /administrators/1/edit
  def edit
    @administrator = Administrator.find(params[:id])
  end

  # POST /administrators
  # POST /administrators.xml
  def create
    @project = Project.find(params[:administrator][:project_id])
    
    @person_becoming_an_admin = get_person_from_ldap_by_username(params[:administrator][:username])
    
    ## the person will be nil if they were not found in LDAP.
    if !@person_becoming_an_admin.nil?
    
      @person_becoming_an_admin.save
    
      ## create an populate the administrator object
      @administrator = Administrator.new()    
      @administrator.project = @project
      @administrator.person = @person_becoming_an_admin
    
      respond_to do |format|
        if @administrator.save 
          flash[:notice] = @administrator.person.full_name + " has been added as an administrator for project: " + @project.name        
          format.html { redirect_to(:controller => 'administrators', :action => 'for_project', :id => @administrator.project.id) }
          format.xml  { render :xml => @administrator, :status => :created, :location => @administrator }
       else
          flash[:notice] = 'Something went wrong.'
          format.html { render :action => "new" }
          format.xml  { render :xml => @administrator.errors, :status => :unprocessable_entity }
      
        end
      end  #respond_to
    else  #if person actuall is nil
      respond_to do |format|
         flash[:notice] = 'The username you provided does not belong to one of our employees. Do not freak out! This message is NOT endorsed by the Human Resources Department.'
         format.html { render :action => "new" }
         format.xml  { render :xml => @administrator.errors, :status => :unprocessable_entity }
      end
    end
    
  end
  
  

  # PUT /administrators/1
  # PUT /administrators/1.xml
  def update
    @administrator = Administrator.find(params[:id])

    respond_to do |format|
      if @administrator.update_attributes(params[:administrator])
        flash[:notice] = 'Administrator was successfully updated.'
        format.html { redirect_to(@administrator) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @administrator.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /administrators/1
  # DELETE /administrators/1.xml
  def destroy
    @administrator = Administrator.find(params[:id])
    
    if @administrator.project.administrators.length <= 1
      flash[:notice] = "You need at least one administrator. If you really want to delete this admin, please add someone else first."
      redirect_to(:controller => 'administrators', :action => 'for_project', :id => @administrator.project.id)
    else
      @administrator.destroy

      respond_to do |format|
        format.html { redirect_to(:controller => 'administrators', :action => 'for_project', :id => @administrator.project.id) }
        format.xml  { head :ok }
      end
    end
  end
end
